<?php

class Tasks extends Model {

	var $NEW_TASK = 1;
	var $IN_PROGRESS_TASK = 2;
	var $UNFINISHED_TASK = 3;
	var $FINISHED_TASK = 4;

	function Tasks() {
		parent::Model();
	}

	function create( $project_id, $creator_id, $title, 
		$description, $start_date, $end_date, $status ) {
		
		$sql = 'insert into `tasks`(`project_id`, `creator_id`, '.
			'`title`, `description`, `start_date`, `end_date`, `status`)'.
			'values(?, ?, ?, ?, ?, ?, ?)';

		$this->db->query( $sql, array( $project_id, $creator_id, $title, 
			$description, $start_date, $end_date, $status));
	}

	function fetch( $task_id ) {
		$sql = 'select * from `tasks` where `task_id`=?';
		$result = $this->db->query($sql, array($task_id) );	
		return $result->row();
	}

	function fetch_by_project( $project_id ) {
		$sql = 'select * from `tasks` where `project_id`=?';
		$result = $this->db->query($sql, array($project_id));
		return $result->result();
	}

	function fetch_by_project_waiting( $project_id ) {
		$sql = 'select * from `tasks` where `project_id`=? and `status` IN (?, ?)';
		$r = $this->db->query($sql, array($project_id, $this->NEW_TASK, $this->UNFINISHED_TASK));
		return $r->result();
	}

	function fetch_by_project_in_progress( $project_id ) {
		$sql = 'select * from `tasks` where `project_id`=? and `status`=?';
		$r = $this->db->query($sql, array($project_id, $this->IN_PROGRESS_TASK));
		return $r->result();
	}

	function fetch_by_project_finished( $project_id ) {
		$sql = 'select * from `tasks` where `project_id`=? and `status`=?';
		$r = $this->db->query($sql, array($project_id, $this->FINISHED_TASK));
		return $r->result();
	}

	function update_status( $task_id, $status ) {
		$sql = 'update into `tasks` set `status`=? where `task_id`=?';
		$this->db->query( $sql, array( $status, $task_id ) );
	}

	function remove( $task_id ) {
		$sql = 'delete from `tasks` where `task_id`=?';
		$this->db->query( $sql, array( $task_id ) );
	}
}

?>
